<?php

namespace apiend\modules\test\controllers;

use Yii;
use apiend\components\Controller;

/**
 * SecurityController for the `test` module
 */
class SecurityController extends Controller {

    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex() {

        /* @var $security \common\base\Security */
        $security = Yii::$app->security;
        $data = "15165109779";

        echo '<br>';
        echo $key = $security->generatePrivateKey($data);
        echo '<br>';
        echo $code = $security->encryptMaster($data);
        echo '<br>';
        echo strlen($code);
        echo '<br>';
        echo $decryptByKey = $security->decryptMaster($code);
        echo '<br>';
        exit;
    }

    private function encript_mobile($mobile, $salt) {
        $mobile_md5 = md5($mobile);
        $figure = preg_replace('/\D/s', '', $mobile_md5);
        $mlen = str_split($figure);
        $pos = array_sum($mlen) % 32 - 1;
        $string = '';
        foreach (str_split($mobile_md5) as $key => $char) {
            $string .= $char;
            if ($key == $pos) {
                $string .= $salt;
            }
        }
        return md5($string);
    }

}
